$(document).ready(function () {
    nsrztcx.onLoad();
})

var nsrztcx = (function () {
    var curSeg;
    var flag = true;
    var init = function () {
        $('#picCode').attr("src", jsptHtp + '/web/cm/showPictureCode.do?t=' + Math.random());
    }

    return {
        onLoad: function () {
            init();
            curSeg = nsrztcx;
            curSeg.onLayui();
        },
        //加载layui
        onLayui: function () {
            layui.use('form', function () {
                var form = layui.form
            });
            layui.use('laydate', function () {
                var laydate = layui.laydate;
                laydate.render({
                    elem: '#wxl_date1',
                    theme: 'molv'
                });
            })
        },

        //加载数据
        onQuery: function () {
            if (flag == false) {
                return false;
            }
            var NSRSBH = $(":input[name='NSRSBH']").val().trim();
            var NSRMC = $(":input[name='NSRMC']").val().trim();
            var rcode = $(":input[name='rcode']").val().trim();

            if (NSRSBH == "" && NSRMC == "") {
                layui.use('layer', function () {
                    var layer = layui.layer;
                    layer.msg("请输入纳税人名称或者纳税人识别号");
                })
                return false;
            }

            if (rcode == "") {
                layui.use('layer', function () {
                    var layer = layui.layer;
                    layer.msg("请输入验证码");
                })
                return false;
            }
            flag = false;

            $(":input[name='rcode']").val('');

            $.ajax({
                url: jsptHtp + "/web/server/platform/sscx/selNsrxxQuery.do",
                type: "GET",
                data: {
                    NSRMC: NSRMC,
                    NSRSBH: NSRSBH,
                    CUR_USERID: "",
                    rcode: rcode,
                },
                dataType: "jsonp",
                jsonpCallback: "nsrztcx.onqueryCallback",
            });
        },

        onqueryCallback: function (jsonObj) {
            flag = true;
            nsrztcx.reloadPicCode();
            switch (jsonObj.code) {
                case "0":
                    if ($.fn.dataTable.isDataTable('#wxlYbnsrcx')) {
                        var table = $('#wxlYbnsrcx').DataTable();
                        table.destroy();
                    }
                    if (jsonObj.data) {
                        if (jsonObj.data[0] || jsonObj.data[1]) {
                            $("#nsrxxQuery").find("span").text("");
                            if (jsonObj.data[0]) {
                                nsrztcx.onBindFormData(jsonObj.data[0], "GS");
                            }
                            if (jsonObj.data[1]) {
                                nsrztcx.onBindFormData(jsonObj.data[1], "DS");
                            }
                        } else {
                            alert("未查询到数据");
                            $("#nsrxxQuery").find("span").text("");
                            return;
                        }
                    } else {
                        alert("未查询到数据");
                        $("#nsrxxQuery").find("span").text("");
                        return;
                    }
                    break;
                case "-1":
                    alert(jsonObj.msg);
                    break;
                default:
                    alert(jsonObj.msg);
                    $("#nsrxxQuery").find("span").text("");
                    return;
            }
        },
        onBindFormData: function (jsonObj, id) {
            for (var key in jsonObj) {
                var obj = $("input[name='" + id + "_" + key + "']");
                if ($(obj).length > 0) {
                    $(obj).val(jsonObj[key]);
                }
                var objSpan = $("span[name='" + id + "_" + key + "']");
                if ($(objSpan).length > 0) {
                    $(objSpan).html(jsonObj[key]);
                }
                var objDiv = $("div[name='" + id + "_" + key + "']");
                if ($(objDiv).length > 0) {
                    $(objDiv).html(jsonObj[key]);
                }
                var objDiv = $("select[name='" + id + "_" + key + "']");
                if ($(objDiv).length > 0) {
                    $(objDiv).val(jsonObj[key]);
                }
            }
        },

        //验证码
        reloadPicCode: function () {
            $('#picCode').attr("src", jsptHtp + '/web/cm/showPictureCode.do?t=' + Math.random());
        },

        winReset: function () {
            $(":input[type=text]").val('');
            $("#nsrxxQuery tbody").empty();
        }
    }
})();